#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <time.h>



// 数字转成字符串
char* Int2String(int num,char *str)//10进制 
{
    int i = 0;//指示填充str 
    if(num<0)//如果num为负数，将num变正 
    {
        num = -num;
        str[i++] = '-';
    } 
    //转换 
    do
    {
        str[i++] = num%10+48;//取num最低位 字符0~9的ASCII码是48~57；简单来说数字0+48=48，ASCII码对应字符'0' 
        num /= 10;//去掉最低位    
    }while(num);//num不为0继续循环
    
    str[i] = '\0';
    
    //确定开始调整的位置 
    int j = 0;
    if(str[0]=='-')//如果有负号，负号不用调整 
    {
        j = 1;//从第二位开始调整 
        ++i;//由于有负号，所以交换的对称轴也要后移1位 
    }
    //对称交换 
    for(;j<i/2;j++)
    {
        //对称交换两端的值 其实就是省下中间变量交换a+b的值：a=a+b;b=a-b;a=a-b; 
        str[j] = str[j] + str[i-1-j];
        str[i-1-j] = str[j] - str[i-1-j];
        str[j] = str[j] - str[i-1-j];
    } 
    
    return str;//返回转换后的值 
}

// 服务器端流程
// socket-->bind-->listen-->accept
int main(){
   
    time_t tmpcal_ptr;
	struct tm *tmp_ptr = NULL;
	
	time(&tmpcal_ptr);
    tmpcal_ptr = time(NULL);   // 两种取值方法均可以
	//printf("tmpcal_ptr=%ld\n", tmpcal_ptr);
	
	tmp_ptr = gmtime(&tmpcal_ptr);
	//printf("after gmtime, the time is:%d:%d:%d\n", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);
	
    // 获取本地时间
	tmp_ptr = localtime(&tmpcal_ptr);
	//printf ("after localtime, the time is:%d.%d.%d ", (1900+tmp_ptr->tm_year), (1+tmp_ptr->tm_mon), tmp_ptr->tm_mday);
	//printf("%d:%d:%d 星期%d\n", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, tmp_ptr->tm_wday==0?7:tmp_ptr->tm_wday);

    char time_year[100]={'0'};
    Int2String(1900+tmp_ptr->tm_year,time_year);
    strcat(time_year,"年");
    //printf("year is %s\n",time_year);
    

    char time_month[100]={'0'};
    Int2String(1+tmp_ptr->tm_mon,time_month);
    strcat(time_month,"月");
    //printf("month is %s\n",time_month);
	
    char time_day[100]={'0'};
    Int2String(tmp_ptr->tm_mday,time_day);
    strcat(time_day,"日");
    //printf("day is %s\n",time_day);

    char time_hour[100]={'0'};
    Int2String(tmp_ptr->tm_hour,time_hour);
    strcat(time_hour,"时");
    //printf("hour is %s\n",time_hour);

    char time_min[100]={'0'};
    Int2String(tmp_ptr->tm_min,time_min);
    strcat(time_min,"分");
    //printf("min is %s\n",time_min);

    char time_sec[100]={'0'};
    Int2String(tmp_ptr->tm_sec,time_sec);
    strcat(time_sec,"秒，");
    //printf("sec is %s\n",time_sec);

    char words[100]="星期";
    char time_week[100]={'0'};
    Int2String(tmp_ptr->tm_wday==0?7:tmp_ptr->tm_wday,time_week);
    strcat(words,time_week);
    strcpy(time_week,words);
    //printf("week is %s\n",time_week);

    char all[1000]="时间：";
    strcat(all,time_year);
    strcat(all,time_month);
    strcat(all,time_day);
    strcat(all,time_hour);
    strcat(all,time_min);
    strcat(all,time_sec);
    strcat(all,time_week);
    //printf("all is %s\n",all);


    
    // 将内容显示在浏览器上

    printf("Content-type: text/html;charset=utf-8\n\n");
    printf("学号：202234241003 姓名：袁伟俊 ");
    printf("%s",all);

    

    return 0;
}
